package org.aeon.config;


import org.aeon.protocol.Serializer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 使用配置文件 获取 编解码方法
 */
public abstract class Config {

    static Properties properties;

    static {
        try (InputStream in = Config.class.getResourceAsStream("/application.properties")) {
            properties = new Properties();
            properties.load(in);
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static Serializer.Algorithm getSerializerAlgorithm() {

        final String value = properties.getProperty("serializer.algorithm");
        if (value == null) {
            // 默认
            return Serializer.Algorithm.JAVA;
        } else {
            // 拼接成  Serializer.Algorithm.JAVA 或 Serializer.Algorithm.JSON
            return Serializer.Algorithm.valueOf(value);
        }
    }

    public static int getServerPort() {
        final String value = properties.getProperty("server.port");
        if (value == null) {
            return 8080;
        } else {
            return Integer.parseInt(value);
        }
    }

    public static String getServerHost() {
        final String value = properties.getProperty("server.host");
        if (value == null) {
            return "127.0.0.1";
        } else {
            return value;
        }
    }
}
